﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Handler.Inventorylistss;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace MyMES.Read.API.Controllers.ZYY.Inventorylistsss
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InventorylistsController : ControllerBase
    {
        private readonly IMediator med;
        private readonly IBaseRepository<Inventorylist> baseRepository;

        /// <summary>
        /// 库存清单列表
        /// </summary>
        /// <param name="med"></param>
        /// <param name="baseRepository"></param>
        public InventorylistsController(IMediator med,IBaseRepository<Inventorylist> baseRepository)
        {
            this.med = med;
            this.baseRepository = baseRepository;
        }
        /// <summary>
        /// 库存清单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<Inventorylist>>> GetInventorylists([FromQuery]GetInventorylistCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await med.Send(request, cancellationToken);
            }
            catch (Exception)
            {

                throw;
            }
          
        }
        /// <summary>
        /// 库存清单导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<IActionResult> ExeclCustomer()
        {
            APIResult<Inventorylist> result = new APIResult<Inventorylist>();
            result.Code = APIEnum.查询成功;
            result.Msg = "客户信息导出成功";
            var sql = "SELECT *  FROM Inventorylist ";
            var Rlist = await baseRepository.SelectAsync(sql);

            //创建工作簿
            IWorkbook workbook = new XSSFWorkbook();

            //依据工作簿创建sheet
            ISheet sheet = workbook.CreateSheet("生产工单信息");
            //依据sheet创建首行
            IRow row = sheet.CreateRow(0);
            //在创建好的行中创建首行单元格
            row.CreateCell(0).SetCellValue("入库时间");
            row.CreateCell(1).SetCellValue("出库时间");
            row.CreateCell(2).SetCellValue("仓库名称");
            row.CreateCell(3).SetCellValue("库存名称");
            row.CreateCell(4).SetCellValue("库存编号");
            row.CreateCell(5).SetCellValue("库存数量");
            row.CreateCell(6).SetCellValue("库存规格");
            row.CreateCell(7).SetCellValue("是否");
   


            //行编号
            int index = 1;
            foreach (var item in Rlist.ToList())
            {
                //sheet表中的第二行完成数据遍历
                //创建新行
                row = sheet.CreateRow(index);

                //将所有数据依次写入到行中
                row.CreateCell(0).SetCellValue(item.InboundTime.ToString("yyyy-MM-dd"));
                row.CreateCell(1).SetCellValue(item.OutboundTime.ToString("yyyy-MM-dd"));
                row.CreateCell(2).SetCellValue(item.WarehouseName);
                row.CreateCell(3).SetCellValue(item.InventoryName);
                row.CreateCell(4).SetCellValue(item.InventoryNumber);
                row.CreateCell(5).SetCellValue(item.InventoryNum);
                row.CreateCell(6).SetCellValue(item.InventSpecifications);
                row.CreateCell(7).SetCellValue(item.Shifou);
             
                //换行
                index++;
            }

            //excel
            byte[] buffer;
            //使用内存流出Excel的内容
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                buffer = ms.ToArray();
            }
            //导出Excel  using Microsoft.AspNetCore.StaticFiles;
            string excelMime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];


            //导出
            return File(buffer, excelMime, "库存清单信息.xlsx");
        }
    }
}
